/**
*  \file tlf.h
*
*  \brief
*
*  \author Dajin Li
*
*
*/
/*===============================================[ private includes  ]================================================*/


#ifndef TLF_H_
#define TLF_H_
#include "../cfg/prj.h"
#include "../app/app.h"
#include <xmc4500/types_local.h>

/*================================================[ public includes  ]================================================*/


/*================================================[ public datatypes ]================================================*/
enum tlf_rd_reg
{
    tlf_RWDCFG0          = 0x1A01,        /* 0  */
    tlf_PROTSTAT         = 0x5000,        /* 1  */
    tlf_RWWDCFG0         = 0x2001,
    tlf_RFWDCFG          = 0x1E00,
    tlf_DEVSTAT          = 0x4E00,
};

enum tlf_wr_reg
{
    tlf_WR_UNLOCK_KEY1    = 0x8756,        /* 0  */
    tlf_WR_UNLOCK_KEY2    = 0x87DE,        /* 1  */
    tlf_WR_UNLOCK_KEY3    = 0x86AD,        /* 0  */
    tlf_WR_UNLOCK_KEY4    = 0x8625,        /* 1  */
    tlf_WR_LOCK_KEY1      = 0x87BE,        /* 0  */
    tlf_WR_LOCK_KEY2      = 0x8668,        /* 1  */
    tlf_WR_LOCK_KEY3      = 0x877D,        /* 0  */
    tlf_WR_LOCK_KEY4      = 0x8795,        /* 1  */
    tlf_DIS_WWDG          = 0x8D27,
    tlf_DIS_ERR_PIN       = 0x8A01,
    tlf_EN_ERR_PIN        = 0x8A10,
    tlf_WWDG_CYCLE1600    = 0x923E,

};



typedef enum
{
    fun_enable = 0x00,
    fun_disable,
} function_op;

/**
 * TLF35584 monitor commond, 
 */
enum tlf_monitor_reg
{
    tlf_MON_SYSFAIL       = 0x3401,                     /*SYSFAIL: Failure status flags,                         Address:0x1A*/         
    tlf_MON_INITERR       = 0x3600,                     /*INITERR: Init error status flags,                      Address:0x1B*/  
    tlf_MON_IF            = 0x3801,                     /*IF     : Interrupt flags,                              Address:0x1C*/
    tlf_MON_SYSSF         = 0x3A00,                     /*SYSSF  : System status flags,                          Address:0x1D*/
    tlf_MON_OTFAIL        = 0x4800,                     /*OTFAIL  : Over temperature failure status flags,       Address:0x24*/
    tlf_MON_OTWRNSF       = 0x4A01,                     /*OTWRNSF : Over temperature warning status flags,       Address:0x25*/
    tlf_MON_MONSF0        = 0x4001,                     /*MONSF0  : Monitor status flags 0,                      Address:0x20*/
    tlf_MON_MONSF1        = 0x4200,                     /*MONSF1  : Monitor status flags 1,                      Address:0x21*/
    tlf_MON_MONSF2        = 0x4400,                     /*MONSF2  : Monitor status flags 2,                      Address:0x22*/
    tlf_MON_MONSF3        = 0x4601,                     /*MONSF3  : Monitor status flags 3,                      Address:0x23*/
    tlf_MON_SPISF         = 0x3E01,                     /*SPISF   : SPI status flags,                            Address:0x1F*/
    tlf_MON_WKSF          = 0x3C00,                     /*WKSF    : Wakeup status flags,                         Address:0x1E*/
};

typedef enum 
{
    tlf_devcfg0          = 0,             /* 0  Device configuration 0 * R2*/
    tlf_devcfg1          = 1,             /* 1  Device configuration 1 * R0*/
    tlf_devcfg2          = 2,             /* 2  Device configuration 2 * R2*/
    tlf_protcfg          = 3,             /* 3  Protection register * R2*/
    tlf_syscfg0          = 4,             /* 4  Protected system configuration request 0 R1*/
    tlf_syscfg1          = 5,             /* 5  Protected system configuration request 1 R2*/
    tlf_wdcfg0           = 6,             /* 6  Protected watchdog configuration request 0 R2*/
    tlf_wdcfg1           = 7,             /* 7  Protected watchdog configuration request 1 R2*/
    tlf_fwdcfg           = 8,             /* 8  Protected functional watchdog configuration request R2*/
    tlf_wwdcfg0          = 9,             /* 9  Protected window watchdog configuration request 0 R2*/
    tlf_wwdcfg1          = 0xA,           /* 10 Protected window watchdog configuration request 1 R2*/
    tlf_rsyscfg0         = 0xB,           /* 11 System configuration 0 status R0*/
    tlf_rsyscfg1         = 0xC,           /* 12 System configuration 1 status R3*/
    tlf_rwdcfg0          = 0xD,           /* 13 Watchdog configuration 0 status R3*/
    tlf_rwdcfg1          = 0xE,           /* 14 Watchdog configuration 1 status R3*/
    tlf_rfwdcfg          = 0xF,           /* 15 Functional watchdog configuration 0 status R3*/
    tlf_rwwdcfg0         = 0x10,          /* 16 Window watchdog configuration 0 status R3*/
    tlf_rwwdcfg1         = 0x11,          /* 17 Window watchdog configuration 1 status R3*/
    tlf_wktimcfg0        = 0x12,          /* 18 Wake timer configuration 0  R3*/
    tlf_wktimcfg1        = 0x13,          /* 19 Wake timer configuration 1  R3*/
    tlf_wktimcfg2        = 0x14,          /* 20 Wake timer configuration 2  R3*/
    tlf_devctrl          = 0x15,          /* 21 Device control request R2*/
    tlf_devctrln         = 0x16,          /* 22 Device control inverted request R2*/
    tlf_wwdscmd          = 0x17,          /* 23 Window watchdog service command R2*/
    tlf_fwdrsp           = 0x18,          /* 24 Functional watchdog response command R2*/
    tlf_fwdrspsync       = 0x19,          /* 25 Functional watchdog response command with synchronizationR2*/
    tlf_sysfail          = 0x1A,          /* 26 Failure status flags R1*/
    tlf_initerr          = 0x1B,          /* 27 Init error status flags R2*/
    tlf_if               = 0x1C,          /* 28 interrupt flags * R2*/
    tlf_syssf            = 0x1D,          /* 29 System status flags * R2*/
    tlf_wksf             = 0x1E,          /* 30 Wakeup status flags * R2*/
    tlf_spisf            = 0x1F,          /* 31 SPI status flags * R2*/
    tlf_monsf0           = 0x20,          /* 32 Monitor status flags 0* R1*/
    tlf_monsf1           = 0x21,          /* 33 Monitor status flags 1* R1*/
    tlf_monsf2           = 0x22,          /* 34 Monitor status flags 2* R1*/
    tlf_monsf3           = 0x23,          /* 35 Monitor status flags 3* R1*/
    tlf_otfail           = 0x24,          /* 36 Over temperature failure status flags* R1*/
    tlf_otwarnsf         = 0x25,          /* 37 Over temperature warn status flags* R2*/
    tlf_vmonstat         = 0x26,          /* 38 Voltage monitor status * R2*/
    tlf_devstat          = 0x27,          /* 39 Device status * R2*/
    tlf_protstat         = 0x28,          /* 40 Protection status * R1*/
    tlf_wwdstat          = 0x29,          /* 41 Window watchdog status * R3*/
    tlf_fwdstat0         = 0x2A,          /* 42 Functional watchdog status 0 * R3*/
    tlf_fwdstat1         = 0x2B,          /* 43 Functional watchdog status 1 * R3*/
    tlf_abist_ctrl0      = 0x2C,          /* 44 ABIST control 0 *R2*/
    tlf_abist_ctrl1      = 0x2D,          /* 45 ABIST control 1 *R2*/
    tlf_abist_select0    = 0x2E,          /* 46 ABIST select 0 *R2*/
    tlf_abist_select1    = 0x2F,          /* 47 ABIST select 1 *R2*/
    tlf_abist_select2    = 0x30,          /* 48 ABIST select 2 *R2*/
    tlf_gtm              = 0x3F,          /* 49 Global testmode *R2*/
    tlf_bck_freq_change  = 0x31,          /* 50 Buck switching frquency change *R2*/
    tlf_bck_fre_spread   = 0x32,          /* 51 Buck frquency spread *R2*/
    tlf_bck_main_ctrl    = 0x33,          /* 52 Buck main ctrl *R2*/
} tlf_reg_addr;

typedef enum 
{
    TLF_NONE = 0,
    TLF_INIT,
    TLF_NORMAL,
    TLF_SLEEP,
    TLF_STANDBY,
    TLF_WAKE,
}tlf_status;

typedef struct 
{
    union
    {
        struct 
        {
            unsigned char pmic_status        : 3;
            unsigned char request_pwr_status : 5;    
        };
        unsigned char devctrl;
    };
} TLF_DEVCTL_t;

typedef enum 
{
    parity_even = 0,
    parity_odd  = 1,
}tlf_parity;

typedef enum 
{
    tlf_read  = 0,
    tlf_write = 1,
}tlf_op;

typedef struct 
{
    union 
    {
        struct 
        {
            tlf_parity parity           :  1;  
            u8 cmd_data                 :  8;   
            tlf_reg_addr reg_addr       :  6;
            tlf_op op                   :  1;
        };
        u16 data_to_send;
    };
} TLF_REGDATA_t;

/*================================================[ public defines   ]================================================*/

#define SYSFAIL_READ_CYCLE                   1 
#define INITERR_READ_CYCLE                   10 
#define IF_READ_CYCLE                        20 
#define SYSSF_READ_CYCLE                     30 
#define OTFAIL_READ_CYCLE                    40 
#define OTWRNSF_READ_CYCLE                   50 
#define MONSF0_READ_CYCLE                    60 
#define MONSF1_READ_CYCLE                    70 
#define MONSF2_READ_CYCLE                    80 
#define MONSF3_READ_CYCLE                    90 
#define SPISF_READ_CYCLE                     100 
#define WKSF_READ_CYCLE                      110 
#define RWWDCFG0_READ_CYCLE_READ_CYCLE       120
#define RWDCFG0_READ_CYCLE_READ_CYCLE        130
#define PROTSTAT_READ_CYCLE_READ_CYCLE       140

#define DEBUG_TEST_TLF35584                  1

#define tlf_trk2en                           0x10
#define tlf_trk1en                           0x08
#define tlf_comen                            0x04
#define tlf_vrefen                           0x01
/*================================================[ public variables ]================================================*/

extern u8 TLF_Reg_sysfail;
extern u8 TLF_Reg_intterr;
extern u8 TLF_Reg_if;
extern u8 TLF_Reg_syssf;
extern u8 TLF_Reg_otfail;
extern u8 TLF_Reg_otwrnsf;
extern u8 TLF_Reg_monsf0;
extern u8 TLF_Reg_monsf1;
extern u8 TLF_Reg_monsf2;
extern u8 TLF_Reg_monsf3;
extern u8 TLF_Reg_spisf;
extern u8 TLF_Reg_wksf;






/*================================================[ inline functions ]================================================*/

/*================================================[ public functions ]================================================*/

extern void tlf_init(void);
extern void tlf_service_10ms(void);

#endif
/***********************************************************************************************************************
*                             Copyright 2016 Linde Material Handling. All rights reserved.                             *
***********************************************************************************************************************/

